import 'package:flutter/material.dart';

/// 年月
class YearMonthWidget extends StatelessWidget {
  /// 视图高度
  final double height;

  /// 视图背景颜色
  final Color bgColor;

  /// 当前显示的年月
  final String ym;

  /// 年月文本 style
  final TextStyle ymStyle;

  /// 左侧切换回调
  final VoidCallback? onLeftChange;

  /// 左侧切换回调
  final VoidCallback? onRightChange;

  const YearMonthWidget({
    super.key,
    required this.ym,
    this.height = 44,
    this.bgColor = Colors.white,
    this.ymStyle = const TextStyle(
      color: Colors.black,
      fontSize: 15,
      fontWeight: FontWeight.bold,
    ),
    this.onLeftChange,
    this.onRightChange,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: height,
      color: bgColor,
      child: Row(
        children: [
          /// 左侧切换按钮
          _ArrowButtonWidget(
            iconName: 'assets/calendar_arrow_left.png',
            onTap: () => onLeftChange?.call(),
          ),

          /// 年月文本
          Expanded(
            child: Container(
              alignment: Alignment.center,
              child: Text(ym, style: ymStyle),
            ),
          ),

          /// 右侧切换按钮
          _ArrowButtonWidget(
            iconName: 'assets/calendar_arrow_right.png',
            onTap: () => onRightChange?.call(),
          ),
        ],
      ),
    );
  }
}

/// 左右箭头切换按钮组件
class _ArrowButtonWidget extends StatelessWidget {
  /// 图片名称
  final String iconName;

  /// 点击回调
  final VoidCallback? onTap;

  const _ArrowButtonWidget({
    Key? key,
    required this.iconName,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        alignment: Alignment.center,
        color: Colors.transparent,
        width: MediaQuery.of(context).size.width / 7,
        child: Image.asset(
          iconName,
          width: 12,
          height: 12,
        ),
      ),
    );
  }
}
